package com.hdw.job.controller;

import javax.servlet.http.HttpServletRequest;

import com.hdw.common.base.api.BaseMsgService;
import com.hdw.common.base.service.BaseMsgServiceImpl;
import com.hdw.common.util.DateUtil;
import com.hdw.job.batch.dao.JdbcJobExecutionExDao;
import com.hdw.job.batch.dao.JdbcStepExecutionExDao;
import com.hdw.job.service.EtlBaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.beans.factory.annotation.Autowired;
import com.hdw.common.base.BaseResult;
import com.hdw.common.constant.CommonResultConstant;
import com.hdw.common.base.BaseException;
import com.hdw.job.api.EtlGroupBatchService;
import com.hdw.job.bean.model.EtlGroupBatch;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;
import com.hdw.common.vo.PaginationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * etlGroupBatch控制器
 * Created by liujunlei on 2022/01/10.
 */
@RestController
@RequestMapping("/job/etlGroupBatch/")
@Api(tags="job批处理")
public class EtlGroupBatchController {

    private static final Logger log = LoggerFactory.getLogger(EtlGroupBatchController.class);

    @Autowired
    EtlGroupBatchService etlGroupBatchService;
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    BaseMsgService baseMsgService;

    @ApiOperation(value="根据组号查询批处理执行")
    @RequestMapping(value="findJobExecutionByIdGroup", method=RequestMethod.GET)
    public BaseResult findJobExecutionByIdGroup(Long idGroup) {
        if(idGroup==null || idGroup==0) {
            return null;
        }
        EtlGroupBatch groupBatch = new EtlGroupBatch();
        groupBatch.setIdGroup(idGroup);
        List<EtlGroupBatch> groupBatchList = etlGroupBatchService.findByModelWithOrder(groupBatch);
        List<Long> idExecutions = new ArrayList<>();
        for(EtlGroupBatch gb:groupBatchList) {
            idExecutions.add(gb.getIdJob());
        }
        JdbcJobExecutionExDao jobExecutionExDao = JdbcJobExecutionExDao.getInstance(jdbcTemplate);
        List<JobExecution> executionList = jobExecutionExDao.findByIds(idExecutions);
        List<Map<String,Object>> returnData = new ArrayList<>();
        Map<String,Object> map;
        for(JobExecution job:executionList) {
            map = new LinkedHashMap<>();
            map.put("id",job.getId());
            map.put("status",job.getStatus().name());
            map.put("tableName",job.getJobParameters().getString("des"));
            map.put("errorMsg",job.getExitStatus().getExitDescription());
            map.put("startDate", DateUtil.FORMAT_HHMMSS.format(job.getStartTime()));
            map.put("endDate",DateUtil.FORMAT_HHMMSS.format(job.getEndTime()));
            map.put("times",(job.getEndTime().getTime()-job.getStartTime().getTime())/1000);
            returnData.add(map);
        }
        return new BaseResult(CommonResultConstant.SUCCESS,returnData);
    }

    @ApiOperation(value="根据组号查询批处理执行步骤")
    @RequestMapping(value="findStepExecutionByIdJob", method=RequestMethod.GET)
    public BaseResult findStepExecutionByIdJob(Long idJobException) {
        JobExecution job = JdbcJobExecutionExDao.getInstance(jdbcTemplate).getJobExecution(idJobException);
        List<StepExecution> stepExecutions = JdbcStepExecutionExDao.getInstance(jdbcTemplate).getStepExecution(job);
        Map<String,Object> map;
        List<Map<String,Object>> returnData = new ArrayList<>();
        for(StepExecution step:stepExecutions) {
            map = new LinkedHashMap<>();
            map.put("stepName",step.getStepName());
            map.put("status",step.getStatus().name());
            map.put("readCount",step.getReadCount());
            map.put("writeCount",step.getWriteCount());
            map.put("skipCount",step.getSkipCount());
            returnData.add(map);
        }
        return new BaseResult(CommonResultConstant.SUCCESS, returnData);
    }
}
